using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Tilver.DBConnectionUI.Editors;

namespace Tilver.DBConnectionUI
{
    internal class UDLEditorsCollection
    {
        Dictionary<string, Type> _editors;
        Dictionary<string, Type> _oleDbeditors;
        public UDLEditorsCollection()
        {
            _editors = new Dictionary<string, Type>();
            _oleDbeditors = new Dictionary<string, Type>();
            this.LoadEditors();
        }

        void LoadEditors()
        {
            this.Add("generic", typeof(GenericEditor));
            this.Add("System.Data.SqlClient", typeof(SqlClientEditor));
            this.Add("SQLOLEDB", typeof(SqlClientEditor));
            this.Add("SQLNCLI", typeof(SqlClientEditor));
            this.Add("System.Data.OracleClient", typeof(OracleEditor));
            this.Add("System.Data.Odbc", typeof(OdbcEditor));
            this.Add("System.Data.OleDb", typeof(OleDbEditor));
            this.Add("Microsoft.Jet.OLEDB.4.0", typeof(OleDbAccessEditor));
            this.Add("Microsoft.Jet.OLEDB.3.51", typeof(OleDbAccessEditor));
            this.Add("Microsoft.ACE.OLEDB.12.0", typeof(OleDbAccessEditor));
            this.Add("System.Data.SqlServerCe", typeof(SqlCompactEditor));
            this.Add("Microsoft.SqlServerCe.Client", typeof(SqlCompactEditor));
            this.Add("System.Data.SqlServerCe.3.0", typeof(SqlCompactEditor));
            this.Add("System.Data.SqlServerCe.3.1", typeof(SqlCompactEditor));
            this.Add("System.Data.SqlServerCe.3.5", typeof(SqlCompactEditor));
            
            this.Add("System.Data.SQLite", typeof(SQLite.SQLiteEditor));
            this.Add("MySql.Data.MySqlClient", typeof(MySQL.MySQLEditor));
            this.Add("MySqlProv", typeof(MySQL.MySQLEditor));
            this.Add("System.Data.Postgres", typeof(Postgre.PostgreEditor));
            this.Add("Npgsql", typeof(Postgre.PostgreEditor));
            this.Add("PostgreSQL", typeof(Postgre.PostgreEditor));
            this.Add("PostgreSQL.1", typeof(Postgre.PostgreEditor));
            this.Add("PostgreSQL OLE DB Provider", typeof(Postgre.PostgreEditor));
            this.Add("FirebirdSql.Data.FirebirdClient", typeof(Firebird.FirebirdEditor));
  
        }

        internal IUDLEditor this[string providerID]
        {
            get
            {
                if (string.IsNullOrEmpty(providerID) || !_editors.ContainsKey(providerID))
                    return null;

                Type editorType = this._editors[providerID];
                IUDLEditor editor = Activator.CreateInstance(editorType) as IUDLEditor;
                return editor;
            }
        }

        internal void Add(string providerID,Type editorType)
        {
            // validate the editorType
            if (!editorType.IsSubclassOf(typeof(System.Windows.Forms.Control)) ||
                (editorType.GetInterface("IUDLEditor") == null))
                throw new ArgumentException("Editor must implement IUDLEditor");
            else
            {
                if (!_editors.ContainsKey(providerID))
                    _editors.Add(providerID, editorType);
            }
        }

        public void Clear()
        {
            _editors.Clear();
        }

    }
}
